package com.jason.fontmetrics.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * @DESC:   给绘制的图形添加一个阴影层效果
 * @Author: Jason
 * @Date: 16/8/30
 * @Time: 00:08
 */

public class ShadowView9 extends View {

    private static final int RECT_SIZE = 800;// 方形大小
    private Paint mPaint;

    private int left, top, right,bottom; //绘制时坐标

    public ShadowView9(Context context, AttributeSet attrs) {
        super(context, attrs);

        // 不支持HW
        setLayerType(LAYER_TYPE_SOFTWARE, null);

        // 初始化画笔
        initPaint();

        // 初始化资源
        initRes();
    }

    private void initRes() {
        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;

        left = width / 2 - RECT_SIZE / 2;
        top = height / 2 - RECT_SIZE / 2;
        right = width / 2 + RECT_SIZE / 2;
        bottom = height / 2 + RECT_SIZE / 2;
    }

    private void initPaint() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setShadowLayer(10, 3, 3, Color.DKGRAY);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制位图
        canvas.drawRect(left, top, right, bottom, mPaint);
    }
}
